package com.cetcs.kmga.dataManager.service.impl.sysManager;

import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.common.Page;
import com.cetcs.kmga.dataManager.dao.mybatis.ServConfigMapper;
import com.cetcs.kmga.dataManager.entity.dataManager.ServConfigPojo;
import com.cetcs.kmga.dataManager.service.sysManager.ServConfigService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采集服务配置服务层
 *
 * @author xutao
 * @version V1.0 创建时间：2017-11-16 15:28
 *          Copyright 2017 by CETC
 */
@Service
public class ServConfigServiceImpl implements ServConfigService {

    @Autowired
    private ServConfigMapper servConfigMapper;

    @Override
    public ServConfigPojo findServConfigById(String id) {
        return servConfigMapper.findServConfigById(id);
    }

    @Override
    public int createServConfig(ServConfigPojo record) {
        Data param = new Data( );
        param.put("servIp", record.getServerIp( ));
        PageBounds bounds = new PageBounds(1, 10);
        PageList<ServConfigPojo> servList = servConfigMapper.findServConfigByCondition(param, bounds);
        if (servList != null && servList.size( ) > 0) {
            return -1;
        } else {
            return servConfigMapper.createServConfig(record);
        }
    }

    @Override
    public int modifyServConfigById(ServConfigPojo record) {
        Data param = new Data( );
        param.put("servIp", record.getServerIp( ));
        PageBounds bounds = new PageBounds(1, 10);
        PageList<ServConfigPojo> servList = servConfigMapper.findServConfigByCondition(param, bounds);
        if (servList != null && servList.size( ) > 0) {
            ServConfigPojo pojo = servList.get(0);
            if (!pojo.getId( ).equals(record.getId( ))) {
                return -1;
            }
        }
        return servConfigMapper.modifyServConfigById(record);
    }

    @Override
    public int modifyServStatusById(String id, String status) {
        return servConfigMapper.modifyServStatusById(id, status, DateUtils.getTime( ));
    }

    @Override
    public int removeServConfigById(String id) {


        return servConfigMapper.removeServConfigById(id);
    }

    @Override
    public int removeServConfigByIds(List<String> ids) {
        String idsStr = String.join(",", ids);
        return servConfigMapper.removeServConfigById(idsStr);
    }

    @Override
    public Page findServConfigByCondition(Data params, Integer page, Integer pageSize) {
        PageBounds bounds = new PageBounds(page, pageSize);
        PageList<ServConfigPojo> servList = servConfigMapper.findServConfigByCondition(params, bounds);
        Page ret = new Page(servList);
        return ret;
    }
}
